﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterManager : MonoBehaviour, IObserver<UIEventType>
{
    public Text attackValue;
    public Text characterAttack;
    public static int characterAttackValue = 0;

    Character character = new Sword();

    private void Awake()
    {
        ObserverManager.ui_observer.AddListener(this, UIEventType.AttackChange);
    }
    void Start()
    {
        characterAttackValue = character.Attack();
        attackValue.text = characterAttackValue.ToString();
        ReadWeaponStrength();
    }

    private void ReadWeaponStrength()
    {
        int num = -1;
        if (PlayerPrefs.HasKey("WeaponStrength"))
        {
            num = PlayerPrefs.GetInt("WeaponStrength");
        }

        switch (num)
        {
            case (int)Weapon.Null:
                characterAttackValue = character.Attack();
                break;
            case (int)Weapon.Sword:
                character = new StrengthSword(character);
                character.UpdateQuality(PlayerPrefs.GetInt("SwordNum"));
                characterAttackValue = character.Attack();
                break;
        }

        GameObject.Find("AddAttack").GetComponent<Button>().onClick.AddListener(() =>
        {
            PlayerPrefs.SetInt("WeaponStrength", 0);

            character = new StrengthSword(character);

            character.UpdateQuality(10);
            characterAttackValue = character.Attack();

            PlayerPrefs.SetInt("SwordNum", character.Attack() - Sword.attack_num);
            //PlayerPrefs.Save();
        });
    }
    private enum Weapon
    { 
        Null = -1,
        Sword = 0,
    }

    private void Update()
    {
        //技能释放  1.技能释放方式 2.技能释放判定 3.伤害判定

        if (Input.GetKeyDown(KeyCode.G))
        {
            int id = Random.Range(0, GetInventoryList.inventoryList.Count);
            Item[] items = GameObject.Find("Panel").transform.GetComponentsInChildren<Item>();
            Item item = null;

            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].isUsed)
                {
                    if (items[i]._name.Equals(GetInventoryList.inventoryList[id].name))
                    {
                        if (items[i].num < GetInventoryList.inventoryList[id].capacity)
                        {
                            item = items[i];
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                else
                {
                    item = items[i];
                    break;
                }
            }

            GetInventoryList.inventoryList[id].num += 1;

            item.GetItem(GetInventoryList.inventoryList[id]);
        }
    }

    public void EventHandler(UniEventArgs<UIEventType> args)
    {
        switch (args.eventType)
        {
            case UIEventType.AttackChange:
                characterAttackValue = character.Attack();
                attackValue.text = characterAttackValue.ToString();
                break;
        }
    }
}
